home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / mac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-22  |  6.6 KB  |  330 lines  |  [TEXT/KAHL]

  1. /* Mac-specific code for Xconq.
  2.    Copyright (C) 1992, 1993, 1994, 1995 Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "conq.h"
  10.  
  11. #ifdef THINK_C
  12. #include <MacHeaders>
  13. #endif /* THINK_C */
  14.  
  15. #ifndef THINK_C /* assume MPW */
  16. #include <Values.h>
  17. #include <Types.h>
  18. #include <Resources.h>
  19. #include <QuickDraw.h>
  20. #include <Fonts.h>
  21. #include <Events.h>
  22. #include <Windows.h>
  23. #include <Menus.h>
  24. #include <TextEdit.h>
  25. #include <Dialogs.h>
  26. #include <Desk.h>
  27. #include <ToolUtils.h>
  28. #include <Memory.h>
  29. #include <SegLoad.h>
  30. #include <Files.h>
  31. #include <Folders.h>
  32. #include <OSUtils.h>
  33. #include <OSEvents.h>
  34. #include <DiskInit.h>
  35. #include <Packages.h>
  36. #include <Traps.h>
  37. #include <Lists.h>
  38. #include <StandardFile.h>
  39. #endif
  40.  
  41.  
  42. #include <signal.h>
  43.  
  44. /* We need this in order to find string resources that have filenames
  45.    in them. */
  46. #include "macdefs.h"
  47.  
  48. /* #include <sys/time.h> */
  49.  
  50. #ifndef c2p
  51. #define c2p(STR,PBUF) \
  52.   strcpy(((char *) PBUF) + 1, STR);  \
  53.   PBUF[0] = strlen(STR);
  54. #endif
  55.  
  56. #ifndef p2c
  57. #define p2c(PSTR,BUF)  \
  58.   strncpy(BUF, ((char *) (PSTR) + 1), PSTR[0]);  \
  59.   BUF[PSTR[0]] = '\0';
  60. #endif
  61.  
  62.  
  63. /* The HFS volume that the program started with. */
  64.  
  65. short initialvrefnum;
  66.  
  67. static char *news_fname;
  68. static char *save_fname;
  69. static char *checkpoint_fname;
  70. static char *error_save_fname;
  71. static char *statistics_fname;
  72.  
  73. #ifndef XCONQLIB
  74. #define XCONQLIB ":lib"
  75. #endif
  76.  
  77. char *
  78. default_library_filename()
  79. {
  80.     return XCONQLIB;
  81. }
  82.  
  83. char *
  84. news_filename()
  85. {
  86.     Str255 tmpstr;
  87.     char tmpbuf[255];
  88.  
  89.     if (news_fname == NULL) {
  90.         GetIndString(tmpstr, sFilenames, siNews);
  91.         p2c(tmpstr, tmpbuf);
  92.         news_fname = copy_string(tmpbuf);
  93.     }
  94.     return news_fname;
  95. }
  96.  
  97. char *
  98. saved_game_filename()
  99. {
  100.     Str255 tmpstr;
  101.     char tmpbuf[255];
  102.  
  103.     if (save_fname == NULL) {
  104.         GetIndString(tmpstr, sFilenames, siSavedGame);
  105.         p2c(tmpstr, tmpbuf);
  106.         save_fname = copy_string(tmpbuf);
  107.     }
  108.     return save_fname;
  109. }
  110.  
  111. char *
  112. checkpoint_filename()
  113. {
  114.     Str255 tmpstr;
  115.     char tmpbuf[255];
  116.  
  117.     if (checkpoint_fname == NULL) {
  118.         GetIndString(tmpstr, sFilenames, siCheckpoint);
  119.         p2c(tmpstr, tmpbuf);
  120.         checkpoint_fname = copy_string(tmpbuf);
  121.     }
  122.     return checkpoint_fname;
  123. }
  124.  
  125. char *
  126. error_save_filename()
  127. {
  128.     Str255 tmpstr;
  129.     char tmpbuf[255];
  130.  
  131.     if (error_save_fname == NULL) {
  132.         GetIndString(tmpstr, sFilenames, siErrorSave);
  133.         p2c(tmpstr, tmpbuf);
  134.         error_save_fname = copy_string(tmpbuf);
  135.     }
  136.     return error_save_fname;
  137. }
  138.  
  139. char *
  140. statistics_filename()
  141. {
  142.     Str255 tmpstr;
  143.     char tmpbuf[255];
  144.  
  145.     if (statistics_fname == NULL) {
  146.         GetIndString(tmpstr, sFilenames, siStatistics);
  147.         p2c(tmpstr, tmpbuf);
  148.         statistics_fname = copy_string(tmpbuf);
  149.     }
  150.     return statistics_fname;
  151. }
  152.  
  153. /* Attempt to open a library file. */
  154.  
  155. FILE *
  156. open_module_library_file(Module *module)
  157. {
  158.     short curvrefnum;
  159.     char fullnamebuf[255];
  160.     FILE *fp = NULL;
  161.     extern short initialvrefnum;
  162.     
  163.     /* Can't open anonymous library modules. */
  164.     if (module->name == NULL)
  165.       return NULL;
  166.     /* Generate library pathname. */
  167.     make_pathname(xconqlib, module->name, "g", fullnamebuf);
  168.     /* Now try to open the file. */
  169.     fp = fopen(fullnamebuf, "r");
  170.     if (fp != NULL) {
  171.         /* Remember the filename where we found it. */
  172.         module->filename = copy_string(fullnamebuf);
  173.     } else {
  174.         GetVol(NULL, &curvrefnum);
  175.         SetVol(NULL, initialvrefnum);
  176.         fp = fopen(fullnamebuf, "r");
  177.         if (fp != NULL) {
  178.             /* Remember the filename (what about volume?) where we found it. */
  179.             module->filename = copy_string(fullnamebuf);
  180.         }
  181.         SetVol(NULL, curvrefnum);
  182.     }
  183.     return fp;
  184. }
  185.  
  186. FILE *
  187. open_module_explicit_file(Module *module)
  188. {
  189.     short curvrefnum;
  190.     char fullnamebuf[255];
  191.     FILE *fp = NULL;
  192.     extern short initialvrefnum;
  193.  
  194.     if (module->filename == NULL) {
  195.         /* Try guessing a filename, since none supplied. */
  196.         if (module->name != NULL) {
  197.             make_pathname(xconqlib, module->name, "g", fullnamebuf);
  198.             fp = fopen(fullnamebuf, "r");
  199.             if (fp != NULL) {
  200.                 return fp;
  201.             } else {
  202.                 GetVol(NULL, &curvrefnum);
  203.                 SetVol(NULL, initialvrefnum);
  204.                 fp = fopen(fullnamebuf, "r");
  205.                 SetVol(NULL, curvrefnum);
  206.             }
  207.         }
  208.     } else {
  209.         sprintf(fullnamebuf, "%s", module->filename);
  210.         fp = fopen(module->filename, "r");
  211.         if (fp != NULL) {
  212.             return fp;
  213.         }
  214.         sprintf(fullnamebuf, ":%s", module->filename);
  215.         fp = fopen(fullnamebuf, "r");
  216.         if (fp != NULL) {
  217.             return fp;
  218.         }
  219.         sprintf(fullnamebuf, "%s%s", ":lib:", module->filename);
  220.         fp = fopen(fullnamebuf, "r");
  221.         if (fp != NULL) {
  222.             return fp;
  223.         }
  224.         /* Try opening a library module under where the program started. */
  225.         GetVol(NULL, &curvrefnum);
  226.         SetVol(NULL, initialvrefnum);
  227.         fp = fopen(fullnamebuf, "r");
  228.         SetVol(NULL, curvrefnum);
  229.     }
  230.     return fp;
  231. }
  232.  
  233. /* (should reindent) */
  234. FILE *
  235. open_library_file(char *filename)
  236. {
  237.     short curvrefnum;
  238.     char fullnamebuf[255];
  239.     FILE *fp;
  240.     extern short initialvrefnum;
  241.     
  242.     /* Now try to open the file. */
  243.     fp = fopen(filename, "r");
  244.     if (fp != NULL)
  245.       return fp;
  246.     /* Generate library pathname. */
  247.     make_pathname(xconqlib, filename, NULL, fullnamebuf);
  248.     fp = fopen(fullnamebuf, "r");
  249.     if (fp != NULL)
  250.       return fp;
  251.     /* Change to volume where the program started. */
  252.     GetVol(NULL, &curvrefnum);
  253.     SetVol(NULL, initialvrefnum);
  254.     fp = fopen(fullnamebuf, "r");
  255.     SetVol(NULL, curvrefnum);
  256.     return fp;
  257. }
  258.  
  259. void
  260. make_pathname(char *path, char *name, char *extn, char *pathbuf)
  261. {
  262.     strcpy(pathbuf, "");
  263.     if (!empty_string(path)) {
  264.     strcat(pathbuf, path);
  265.     strcat(pathbuf, ":");
  266.     }
  267.     strcat(pathbuf, name);
  268.     /* Don't add a second identical extension, but do add if extension
  269.        is different (in case we want "foo.12" -> "foo.12.g" for instance) */
  270.     if (strrchr(name, '.')
  271.     && extn
  272.     && strcmp(strrchr(name, '.') + 1, extn) == 0)
  273.       return;
  274.     if (!empty_string(extn)) {
  275.     strcat(pathbuf, ".");
  276.     strcat(pathbuf, extn);
  277.     }
  278. }
  279.  
  280. /* Remove a saved game from the system. */
  281.  
  282. void
  283. remove_saved_game()
  284. {
  285. }
  286.  
  287. void
  288. init_signal_handlers()
  289. {
  290. }
  291.  
  292. int last_ticks = 0;
  293.  
  294. int
  295. n_seconds_elapsed(n)
  296. int n;
  297. {
  298.     int ticks = TickCount();
  299.  
  300.     if (((ticks - last_ticks) / 60) > n) {
  301.         last_ticks = ticks;
  302.         return TRUE;
  303.     } else {
  304.         return FALSE;
  305.     }
  306. }
  307.  
  308. /* Instead of coredumping, which is not a normal Mac facility, we
  309.    drop into Macsbug.  If we then "g" from Macsbug, the program will
  310.    exit cleanly. */
  311.  
  312. void
  313. mac_abort ()
  314. {
  315.   /* Make sure no output still buffered up, then zap into MacsBug. */
  316. #if 0 /* how to know if stdio in use? */
  317.   fflush(stdout);
  318.   fflush(stderr);
  319.   printf("## Abort! ##\n");
  320. #endif
  321. #ifdef MPW_SADE
  322.   SysError(8005);
  323. #else 
  324.   Debugger();
  325. #endif
  326.   /* "g" in MacsBug will then cause a regular error exit. */
  327.   exit(1);
  328. }
  329.  
  330.